/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.yxw.live_vod_boot.jobs;

import com.yxw.live_vod_boot.cache.QueueCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.yxw.live_vod_boot.dto.YxLiveWatchLogAddDTO;
import com.yxw.live_vod_boot.entity.YxLiveWatchLogEntity;
import com.yxw.live_vod_boot.service.impl.LiveGeneraUserProcessServiceImpl;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.ArrayList;
import java.util.UUID;
/*** 
* 用于直播观看记录的数据库写入
*@author hjx
*@date 2023/12/1 14:06:22
*@update 2023/12/14 14:06:22
*
**/
@Slf4j
@Component
@EnableScheduling
public class LiveWatchLogJob {

    private static AtomicBoolean bWriteToDbIsOver = new AtomicBoolean(false);
    private static long execStartTime = 0;
    private static ArrayList<YxLiveWatchLogAddDTO> protectJobList = new ArrayList();
    @Autowired
    private LiveGeneraUserProcessServiceImpl oLiveGeneraUserProcessServiceImpl;
    @Autowired
    private QueueCache oQueueCache;
    
    //@Async
    @Scheduled(fixedRate = 10)
    public void liveWatchLogSave()
    {
        //每天86400000 毫秒       1天 
        //理论每天最多写入1728000000条数据
        if(!QueueCache.serviceIsOk())
        {
            return;
        }
        if(null == QueueCache.getLiveWatchLogQueue())
        {
            System.out.println("QueueCache.oLiveWatchLogQueue is null");
            return;
        }
        if(bWriteToDbIsOver.get())
        {
            return;
        }
        bWriteToDbIsOver.set(true);
        execStartTime = System.currentTimeMillis();
        ArrayList<YxLiveWatchLogEntity> watchLogList = new ArrayList();
        protectJobList = new ArrayList();
        for(int i=0;i<200;i++)
        {
            YxLiveWatchLogAddDTO oYxLiveWatchLogAddDTO = QueueCache.liveWatchLogPoll();
            if(null != oYxLiveWatchLogAddDTO)
            {
                YxLiveWatchLogEntity watchLog = oYxLiveWatchLogAddDTO.ConverToEntity();                
                watchLogList.add(watchLog);
                protectJobList.add(oYxLiveWatchLogAddDTO);
            }
        }
        if(!watchLogList.isEmpty())
        {
            
            System.out.println("liveWatchLogSave ---------------------------------------------------------start at:" + execStartTime);
            BaseResult oBaseResult = oLiveGeneraUserProcessServiceImpl.liveWatchLogAddToDb(watchLogList);
            if(!oBaseResult.isResult())
            {                
                oQueueCache.liveWatchLogQueueAddAll(protectJobList);
            }
            System.out.println("liveWatchLogSave ---------------------------------------------------------end at:" + System.currentTimeMillis());
            System.out.println("liveWatchLogSave ---------------------------------------------------------totall data:" + watchLogList.size());
        }
        bWriteToDbIsOver.set(false);
    }
    //监控保护任务，10分钟检查一次
    @Scheduled(fixedRate = 600000)
    public void liveWatchLogCheckProtectJob()
    { 
        if(bWriteToDbIsOver.get())
        {
            long tmpTime = System.currentTimeMillis() - execStartTime;
            if(tmpTime >= 590000)
            {
                if(!protectJobList.isEmpty())
                {
                    oQueueCache.liveWatchLogQueueAddAll(protectJobList);
                }
                bWriteToDbIsOver.set(false);
            }
        }
    }
}